package com.rayboo.example.collections.list;

import java.util.Enumeration;
import java.util.Vector;

/**
 * Vector 示例
 *
 * @author ray
 * @since 2019/07/25
 *
 * <p>Vector实现List接口。
 * 就像ArrayList它也维护插入顺序一样，但它很少在非线程环境中使用，因为它是同步的，并且由于它在搜索，添加，删除和更新其元素时表现不佳。
 *
 * <p>https://beginnersbook.com/2013/12/vector-in-java/
 */
public class VectorExample {

    public static void main(String args[]) {
        /* Vector of initial capacity(size) of 2 */
        Vector<String> vec = new Vector<String>(2);

        /* Adding elements to a vector*/
        vec.addElement("Apple");
        vec.addElement("Orange");
        vec.addElement("Mango");
        vec.addElement("Fig");

        /* check size and capacityIncrement*/
        System.out.println("Size is: " + vec.size());
        System.out.println("Default capacity increment is: " + vec.capacity());

        vec.addElement("fruit1");
        vec.addElement("fruit2");
        vec.addElement("fruit3");

        /*size and capacityIncrement after two insertions*/
        System.out.println("Size after addition: " + vec.size());
        System.out.println("Capacity after increment is: " + vec.capacity());

        /*Display Vector elements*/
        Enumeration en = vec.elements();
        System.out.println("\nElements are:");
        while (en.hasMoreElements())
            System.out.print(en.nextElement() + " ");
    }
}
